package 用两个栈实现队列;

import org.omg.PortableInterceptor.INACTIVE;

import java.util.*;

/**
 * 自己做的版本
 */
public class CQueueV1 {

    public int[] stackA;
    public int[] stackB;
    public int stackATopIndex;
    public int stackBTopIndex;

    public CQueueV1() {
        this.stackA = new int[10000];
        this.stackB = new int[10000];
        this.stackATopIndex = 0;
        this.stackBTopIndex = 0;
    }

    public void appendTail(int value) {
        putA(value);
    }

    public int deleteHead() {
        for(int i = stackATopIndex - 1; i >= 0; i--) {
            putB(popA());
        }
        int headValue = popB();
        for(int i = stackBTopIndex - 1; i >= 0; i--) {
            putA(popB());
        }
        return headValue;
    }

    private void putA(int value) {
        stackA[stackATopIndex] = value;
        stackATopIndex++;
    }

    private int popA() {
        if(stackATopIndex <= 0) {
            return -1;
        } else {
            stackATopIndex--;
            return stackA[stackATopIndex];
        }
    }

    private void putB(int value) {
        stackB[stackBTopIndex] = value;
        stackBTopIndex++;
    }

    private int popB() {
        if(stackBTopIndex <= 0) {
            return -1;
        } else {
            stackBTopIndex--;
            return stackB[stackBTopIndex];
        }
    }

    public void printSnapshot() {
        System.out.println("stackA: " + Arrays.toString(stackA));
        System.out.println("stackB: " + Arrays.toString(stackB));
        System.out.println("indexA: " + stackATopIndex);
        System.out.println("indexB: " + stackBTopIndex);
    }
}
